package com.example.cpl.gjc;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class format_test extends AppCompatActivity {
    //声明控件
    private static final String TAG = "TAG";
    private static final String IP_HEAD = "192.168.31.";
    private Button btn_Back,button_Connect, button_t1, button_t2,button_t3,button_t4,button_t5,button_t6, button_t7,
            button_t8,button_t9,button_t10,button_t11,button_t12,button_t13,button_t14,button_t15,button_t16,
            button_t17,button_t18,button_t19,button_t20,button_t21,button_t22,button_t23,button_t24,button_t25,
            button_t26,button_t27,button_t28,button_t29,button_t30,button_t31,button_t32,button_t33,button_t34,button_t35,button_t36,
            button_t37,button_t38,button_t39,button_t40,button_add,button_minus,btn_IDConnect,btn_IDminus,btn_IDadd;
    private TextView tv_time,tv_ID;
    static Handler mHandler;
    static ExecutorService mThreadPool;
    static Socket socket;  // Socket变量
    static OutputStream outputStream;
    private int wi=0,time=10,id=1,
            t1=0,t2=0,t3=0,t4=0,t5=0,t6=0,t7=0,t8=0,t9=0,t10=0,t11=0,t12=0,t13=0,t14=0,
            t15=0,t16=0,t17=0,t18=0,t19=0,t20=0,t21=0,t22=0,t23=0,t24=0,t25=0,t26=0,t27=0,t28=0,t29=0,t30=0,t31=0,t32=0,t33=0,t34=0,
            t35=0,t36=0,t37=0,t38=0,t39=0,t40=0;
    //    String rec;        // 接收服务器发送过来的消息
    InputStream is;         // 输入流对象
    InputStreamReader isr ; // 输入流读取器对象
    static BufferedReader br ;     // 输入流读取器对象
    public int m_count=0,aa=0; //声明进度条对话框

    ProgressDialog m_pDialog;
    //    AlertDialog mess;
    public void initWidght(){
        btn_Back=(Button)findViewById(R.id.form_back);

        button_Connect=(Button)findViewById(R.id.form_button_Connect);
        button_add=(Button)findViewById(R.id.form_button_add);
        button_minus=(Button)findViewById(R.id.form_button_minus);
        btn_IDConnect=(Button)findViewById(R.id.form_btn_IDConnect);
        btn_IDminus=(Button)findViewById(R.id.form_btn_IDminus);
        btn_IDadd=(Button)findViewById(R.id.form_btn_IDadd);
        tv_ID=(TextView)findViewById(R.id.form_tv_ID1);
        tv_time=(TextView)findViewById(R.id.form_tv_time1);
        button_t1=(Button)findViewById(R.id.form_button_t1);
        button_t2=(Button)findViewById(R.id.form_button_t2);
        button_t3=(Button)findViewById(R.id.form_button_t3);
        button_t4=(Button)findViewById(R.id.form_button_t4);
        button_t5=(Button)findViewById(R.id.form_button_t5);
        button_t6=(Button)findViewById(R.id.form_button_t6);
        button_t7=(Button)findViewById(R.id.form_button_t7);
        button_t8=(Button)findViewById(R.id.form_button_t8);
        button_t9=(Button)findViewById(R.id.form_button_t9);
        button_t10=(Button)findViewById(R.id.form_button_t10);
        button_t11=(Button)findViewById(R.id.form_button_t11);
        button_t12=(Button)findViewById(R.id.form_button_t12);
        button_t13=(Button)findViewById(R.id.form_button_t13);
        button_t14=(Button)findViewById(R.id.form_button_t14);
        button_t15=(Button)findViewById(R.id.form_button_t15);
        button_t16=(Button)findViewById(R.id.form_button_t16);
        button_t17=(Button)findViewById(R.id.form_button_t17);
        button_t18=(Button)findViewById(R.id.form_button_t18);
        button_t19=(Button)findViewById(R.id.form_button_t19);
        button_t20=(Button)findViewById(R.id.form_button_t20);

        button_t21=(Button)findViewById(R.id.form_button_t21);
        button_t22=(Button)findViewById(R.id.form_button_t22);
        button_t23=(Button)findViewById(R.id.form_button_t23);
        button_t24=(Button)findViewById(R.id.form_button_t24);
        button_t25=(Button)findViewById(R.id.form_button_t25);
        button_t26=(Button)findViewById(R.id.form_button_t26);
        button_t27=(Button)findViewById(R.id.form_button_t27);
        button_t28=(Button)findViewById(R.id.form_button_t28);

        button_t29=(Button)findViewById(R.id.form_button_t29);
        button_t30=(Button)findViewById(R.id.form_button_t30);
        button_t31=(Button)findViewById(R.id.form_button_t31);
        button_t32=(Button)findViewById(R.id.form_button_t32);
        button_t33=(Button)findViewById(R.id.form_button_t33);
        button_t34=(Button)findViewById(R.id.form_button_t34);
        button_t35=(Button)findViewById(R.id.form_button_t35);
        button_t36=(Button)findViewById(R.id.form_button_t36);
        button_t37=(Button)findViewById(R.id.form_button_t37);
        button_t38=(Button)findViewById(R.id.form_button_t38);
        button_t39=(Button)findViewById(R.id.form_button_t39);
        button_t40=(Button)findViewById(R.id.form_button_t40);
    }

    public void ProgressShow(int num)
    {
        m_count = 0;
        m_pDialog = new ProgressDialog(format_test.this);// 创建ProgressDialog对象
        m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置进度条风格，风格为长形
        m_pDialog.setTitle("发送中..."); // 设置ProgressDialog 标题
        m_pDialog.setMessage("正在分发题目 请等待");  // 设置ProgressDialog 提示信息
        m_pDialog.setIcon(R.drawable.icon); // 设置ProgressDialog 标题图标
        m_pDialog.setMax(num);// 设置进度条最大值
//        m_pDialog.setCancelable(Boolean.parseBoolean("取消"));
//        m_pDialog.setProgress(50);// 设置ProgressDialog 进度条进度
        m_pDialog.setIndeterminate(false);  // 设置ProgressDialog 的进度条是否不明确
        m_pDialog.setCancelable(false); // 设置ProgressDialog 是否可以按退回按键取消
        m_pDialog.show(); // 让ProgressDialog显示
    }
//    public void Connect_WIFI_TCP(String  ssid, String password,int type,final String ip, final int port) {
//        Log.d(TAG,"WiFi连接中...");
//        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
//        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//        WifiAdmin wifiAdmin = new WifiAdmin(button_1.this);
//
//        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//        if(   !(mWifi.isConnected())    ||   (!(wifiInfo.getSSID().equals("\"" +ssid+"\"")))){
//            if(wifiInfo.getSSID().equals("<unknown ssid>")) {    //这个条件说明没有打开WiFi
//                wifiAdmin.openWifi();
//            }
//            wifiAdmin.disconnectWifi(wifiAdmin.getNetworkId());
//            wifiAdmin.addNetwork(wifiAdmin.createWifiConfig("" + ssid + "", "" + password + "", type));
//            //这里加延时3秒
//            Log.d(TAG,"延时开始");
//            try {
//                Thread.sleep(3500);
//            } catch (InterruptedException e){
//                e.printStackTrace();
//            }Log.d(TAG,"延时结束");
//
//            Log.d(TAG,"判断WiFi是否连接");
//            if((wi<1)&& ((!wifiInfo.getSSID().equals("\"" +ssid+"\""))||(!(mWifi.isConnected()))) )
//            {
//                wi++;
//                Log.d(TAG,"WiFi连接失败 尝试重新连接.....");
//                Log.d(TAG,"WiFi重新连接次数"+wi);
//                Connect_WIFI_TCP("" + ssid + "", "" + password + "", type,""+ip+"", port);
//
//            }
//            wi=0;
//        }
//
//        if ((mWifi.isConnected())){           //去掉了这个条件(wifiInfo.getSSID().equals("\"" +ssid+"\""))&&
//            Log.d(TAG, "WiFi连接成功!");
//            Connect_Tcp(""+ip+"", port);
//            //这里加入延时 给TCP反应时间   延迟下一个WiFi的连接
//        }
//        else {
//            Log.d(TAG, "——WiFi:  "+ ssid + "  没有找到........0.0");
//        }
//    }

    public void Connect_Tcp(final String ip, final int port) {
        Log.d(TAG,"TCP连接中...");
//        mThreadPool.execute(new Runnable() {
//            @Override
//            public void run() {
        try {

            socket = new Socket(""+ip+"",port);
            Log.d(TAG,"TCP连接成功");
            is = socket.getInputStream();             // 步骤1：创建输入流对象InputStream
            isr = new InputStreamReader(is);          // 步骤2：创建输入流读取器对象 并传入输入流对象
            br = new BufferedReader(isr);             // 该对象作用：发送数据
            outputStream = socket.getOutputStream();  // 步骤2：写入需要发送的数据到输出流对象中
            outputStream.write(("bbbbbbbbbb,"+time+","+t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12+t13+t14+t15+t16+t17+t18+t19+t20+t21+t22+t23+t24+t25+t26+t27+t28+t29+t30+t31+t32+t33+t34+t35+t36+t37+t38+t39+t40).getBytes("utf-8"));    // 数据的结尾加上换行符才可让服务器端的readline()停止阻塞
            //Log.d(TAG,"TCP数据"+time+","+t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12+t13+t14+t15+t16+t17+t18+t19+t20+t21+t22+t23+t24+t25+t26+t27+t28+t29+t30+t31+t32+t33+t34+t35+t36+t37+t38+t39+t40);
            outputStream.flush();
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e){
//                e.printStackTrace();
//            }
            //Log.d(TAG,"延时结束");
            //发送进入正式考核
//            outputStream.write(("bbbbbbbbbb").getBytes("utf-8"));    // 数据的结尾加上换行符才可让服务器端的readline()停止阻塞
//                    outputStream.write(("start" +wi+ "\n").getBytes("utf-8"));    //可以发送int类型的
            // 步骤3：发送数据到服务端
//            outputStream.flush();
            Log.d(TAG,"TCP数据发送成功");
            socket.close();          //发送完数据后断开连接
            Log.d(TAG,"TCP已成功断开!");
            m_count+=1;
            m_pDialog.setProgress(m_count);
        } catch (IOException e) {

            e.printStackTrace();
        }

        Log.d(TAG,"——进度条进度为 "+m_count+"/50");
//            }
//        });
    }

    @SuppressLint("HandlerLeak")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_format_test);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // mVideoView= (VideoView) findViewById(R.id.videoView1);

//         Uri vUri = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.hn);
//        mVideoView.setVideoURI(vUri);
//        mVideoView.setMediaController(new MediaController(button_1.this));
//        mVideoView.start();

//        MediaController mc = new MediaController(this);     //隐藏videoview进度条或者控制部分
//        mc.setVisibility(View.INVISIBLE);
//        mVideoView.setMediaController(mc);

        //监听视频播放完的代码
//        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mPlayer) {
//                // TODO Auto-generated method stub
//                mPlayer.start();
//                mPlayer.setLooping(true);           //循环播放
//            }
//        });
        initWidght(); //关联
        tv_time.setText(""+time+"");
        tv_ID.setText(""+id+"");
        mThreadPool = Executors.newCachedThreadPool();   // 初始化线程池
        mHandler = new Handler()
        {                    // 实例化主线程,用于更新接收过来的消息
            @Override
            public void handleMessage(android.os.Message msg)
            {
                Log.d(TAG, "接收obj= "+msg.obj);
                Log.d(TAG, "接收what= "+msg.what);
                switch (msg.what){
                    case 0x01:
                        AlertDialog dlg = new AlertDialog.Builder(format_test.this)
                                .setIcon(R.drawable.icon)
                                .setTitle("发送状态")
                                .setMessage("成功：" + m_count + "台，失败：" + (22 - m_count) + "台！")

                                .setPositiveButton("确定", null)
                                .create();
                        dlg.show();
                        msg.what=0;
                        break;
                    case 0x02:
                        AlertDialog dlg1 = new AlertDialog.Builder(format_test.this)
                                .setIcon(R.drawable.icon)
                                .setTitle("发送状态")
//                                    .setMessage("成功：" + m_count + "台，失败：" + (1 - m_count) + "台！")
                                .setMessage( m_count>0? "                                        "+""+id+""+"号机发送成功！" : "                                        "+""+id+""+"号机发送失败..." )
                                .setPositiveButton("确定", null)
                                .create();
                        dlg1.show();
                        msg.what=0;
                        break;
                }
            }
        };
        button_Connect.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                ProgressShow(25);
                new Thread()
                {
                    public void run(){
//                        Message msg=new Message();
//                        msg.what=0x01;//标识
//                        msg.obj="发送中...";



                        for(int a=1;a<25;a++) {
                            Connect_Tcp(IP_HEAD + (a+100), 8080);
                        }


                        m_pDialog.cancel();     //关闭进度条对话框

                        Message msg=new Message();
                        msg.what=0x01;
//                        msg.arg1=1;
                        mHandler.sendMessage(msg);
                    }
                }.start();


            }


        });

        btn_IDConnect.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                ProgressShow(1);
                new Thread()
                {
                    public void run(){
//                        Message msg=new Message();
//                        msg.what=0x01;//标识
//                        msg.obj="发送中...";




                        Connect_Tcp(IP_HEAD+ (id+100), 8080);





                        m_pDialog.cancel();     //关闭进度条对话框
                        Message msg=new Message();
                        msg.what=0x02;
//                        msg.arg1=1;
                        mHandler.sendMessage(msg);
                    }
                }.start();
            }


        });
        button_add.setOnClickListener(new View.OnClickListener()
        {
            //            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view)
            {
                time+=5;
                if(time>90)
                {
                    time=90;
                }
                tv_time.setText(""+time+"");
                Log.d(TAG,"——设置的时间= "+time);
            }
        });
        button_minus.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                time-=5;
                if(time<10)
                {
                    time=10;
                }
                tv_time.setText(""+time+"");
                Log.d(TAG,"——设置的时间= "+time);
            }
        });

        btn_IDadd.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                id+=1;
                if(id>25)
                {
                    id=25;
                }
                tv_ID.setText(""+id+"");
                Log.d(TAG,"——设置的ID为= "+id);
            }
        });

        btn_IDminus.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                id-=1;
                if(id<1)
                {
                    id=1;
                }
                tv_ID.setText(""+id+"");
                Log.d(TAG,"——设置的ID为= "+id);
            }
        });

        btn_Back.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(format_test.this, MainActivity.class);
                startActivity(intent);
//                overridePendingTransition(R.anim.abc_slide_out_bottom, R.anim.abc_slide_in_bottom);
            }
        });
        button_t1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t2==1) {
//                button_t2.performClick();
//            }
                t1=(t1+1)%2;      //点击然后改变flag的值
                button_t1.getBackground().setColorFilter(Color.parseColor(t1 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
//                button_t1.setText(t1 >0?"按下":"1");//按下改变text值 弹起恢复
                Log.d(TAG,"——t1= "+t1);

            }
        });
        button_t2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t1==1){
//                button_t1.performClick();
//            }
                t2=(t2+1)%2;      //点击然后改变flag的值
                button_t2.getBackground().setColorFilter(Color.parseColor(t2 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
//                button_t2.setText(t2 >0?"按下":"2");
                Log.d(TAG,"——t2= "+t2);

            }
        });
        button_t3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t3=(t3+1)%2;      //点击然后改变flag的值
                button_t3.getBackground().setColorFilter(Color.parseColor(t3 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);

//                button_t3.setText(t3 >0?"按下":"3");
                Log.d(TAG,"——t3= "+t3);
            }
        });
        button_t4.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t8==1)
//            {button_t8.performClick();}
                t4=(t4+1)%2;      //点击然后改变flag的值
                button_t4.getBackground().setColorFilter(Color.parseColor(t4 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t4= "+t4);
            }
        });
        button_t5.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t6==1)
//            {button_t6.performClick();}
                t5=(t5+1)%2;      //点击然后改变flag的值
                button_t5.getBackground().setColorFilter(Color.parseColor(t5 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t5= "+t5);
            }
        });
        button_t6.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t5==1)
//            {button_t5.performClick();}
                t6=(t6+1)%2;      //点击然后改变flag的值
                button_t6.getBackground().setColorFilter(Color.parseColor(t6 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t6= "+t6);
            }
        });
        button_t7.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t11==1)
//            {button_t11.performClick();}
                t7=(t7+1)%2;      //点击然后改变flag的值
                button_t7.getBackground().setColorFilter(Color.parseColor(t7 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t7= "+t7);
            }
        });
        button_t8.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t4==1)
//            {button_t4.performClick();}
                t8=(t8+1)%2;      //点击然后改变flag的值
                button_t8.getBackground().setColorFilter(Color.parseColor(t8 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t8= "+t8);
            }
        });
        button_t9.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t10==1) {
//                    button_t10.performClick();
//                }
                t9=(t9+1)%2;      //点击然后改变flag的值
                button_t9.getBackground().setColorFilter(Color.parseColor(t9 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t9= "+t9);
            }
        });
        button_t10.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t9==1)
//            {button_t9.performClick();}
                t10=(t10+1)%2;      //点击然后改变flag的值
                button_t10.getBackground().setColorFilter(Color.parseColor(t10 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t10= "+t10);
            }
        });
        button_t11.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t7==1)
//            {button_t7.performClick();}
                t11=(t11+1)%2;      //点击然后改变flag的值
                button_t11.getBackground().setColorFilter(Color.parseColor(t11 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t11= "+t11);
            }
        });
        button_t12.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t12=(t12+1)%2;      //点击然后改变flag的值
                button_t12.getBackground().setColorFilter(Color.parseColor(t12 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t12= "+t12);
            }
        });
        button_t13.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t14==1)
//            {button_t14.performClick();}
                t13=(t13+1)%2;      //点击然后改变flag的值
                button_t13.getBackground().setColorFilter(Color.parseColor(t13 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t13= "+t13);
            }
        });
        button_t14.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t13==1)
//            {button_t13.performClick();}
                t14=(t14+1)%2;      //点击然后改变flag的值
                button_t14.getBackground().setColorFilter(Color.parseColor(t14 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t14= "+t14);
            }
        });
        button_t15.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t15=(t15+1)%2;      //点击然后改变flag的值
                button_t15.getBackground().setColorFilter(Color.parseColor(t15 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t15= "+t15);
            }
        });
        button_t16.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t16=(t16+1)%2;      //点击然后改变flag的值
                button_t16.getBackground().setColorFilter(Color.parseColor(t16 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t16= "+t16);
            }
        });
        button_t17.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t17=(t17+1)%2;      //点击然后改变flag的值
                button_t17.getBackground().setColorFilter(Color.parseColor(t17 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t17= "+t17);
            }
        });
        button_t18.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t18=(t18+1)%2;      //点击然后改变flag的值
                button_t18.getBackground().setColorFilter(Color.parseColor(t18 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t18= "+t18);
            }
        });
        button_t19.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t19=(t19+1)%2;      //点击然后改变flag的值
                button_t19.getBackground().setColorFilter(Color.parseColor(t19 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t19= "+t19);
            }
        });
        button_t20.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t20=(t20+1)%2;      //点击然后改变flag的值
                button_t20.getBackground().setColorFilter(Color.parseColor(t20 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t20= "+t20);
            }
        });
        button_t21.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t21=(t21+1)%2;      //点击然后改变flag的值
                button_t21.getBackground().setColorFilter(Color.parseColor(t21 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t21= "+t21);
            }
        });
        button_t22.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t22=(t22+1)%2;      //点击然后改变flag的值
                button_t22.getBackground().setColorFilter(Color.parseColor(t22 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t22= "+t22);
            }
        });
        button_t23.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t23=(t23+1)%2;      //点击然后改变flag的值
                button_t23.getBackground().setColorFilter(Color.parseColor(t23 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t23= "+t23);
            }
        });
        button_t24.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t24=(t24+1)%2;      //点击然后改变flag的值
                button_t24.getBackground().setColorFilter(Color.parseColor(t24 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t24= "+t24);
            }
        });
        button_t25.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t25=(t25+1)%2;      //点击然后改变flag的值
                button_t25.getBackground().setColorFilter(Color.parseColor(t25 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t25= "+t25);
            }
        });
        button_t26.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t26=(t26+1)%2;      //点击然后改变flag的值
                button_t26.getBackground().setColorFilter(Color.parseColor(t26 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t26= "+t26);
            }
        });
        button_t27.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t28==1) {
//                    button_t28.performClick();
//                }
                t27=(t27+1)%2;      //点击然后改变flag的值
                button_t27.getBackground().setColorFilter(Color.parseColor(t27 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t27= "+t27);
            }
        });
        button_t28.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t27==1) {
//                    button_t27.performClick();
//                }
                t28=(t28+1)%2;      //点击然后改变flag的值
                button_t28.getBackground().setColorFilter(Color.parseColor(t28 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t28= "+t28);
            }
        });


        button_t29.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t27==1) {
//                    button_t27.performClick();
//                }
                t29=(t29+1)%2;      //点击然后改变flag的值
                button_t29.getBackground().setColorFilter(Color.parseColor(t29 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t29= "+t29);
            }
        });

        button_t30.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
//                if(t27==1) {
//                    button_t27.performClick();
//                }
                t30=(t30+1)%2;      //点击然后改变flag的值
                button_t30.getBackground().setColorFilter(Color.parseColor(t30 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t30= "+t30);
            }
        });

        button_t31.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t31=(t31+1)%2;      //点击然后改变flag的值
                button_t31.getBackground().setColorFilter(Color.parseColor(t31 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t31= "+t31);
            }
        });

        button_t32.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t32=(t32+1)%2;      //点击然后改变flag的值
                button_t32.getBackground().setColorFilter(Color.parseColor(t32 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t32= "+t32);
            }
        });
        button_t33.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t33=(t33+1)%2;      //点击然后改变flag的值
                button_t33.getBackground().setColorFilter(Color.parseColor(t33 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t33= "+t33);
            }
        });
        button_t34.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t34=(t34+1)%2;      //点击然后改变flag的值
                button_t34.getBackground().setColorFilter(Color.parseColor(t34 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t34= "+t34);
            }
        });

        button_t35.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t35=(t35+1)%2;      //点击然后改变flag的值
                button_t35.getBackground().setColorFilter(Color.parseColor(t35 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t35= "+t35);
            }
        });
        button_t36.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t36=(t36+1)%2;      //点击然后改变flag的值
                button_t36.getBackground().setColorFilter(Color.parseColor(t36 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t36= "+t36);
            }
        });

        button_t37.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t37=(t37+1)%2;      //点击然后改变flag的值
                button_t37.getBackground().setColorFilter(Color.parseColor(t37 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t37= "+t37);
            }
        });


        button_t38.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t38=(t38+1)%2;      //点击然后改变flag的值
                button_t38.getBackground().setColorFilter(Color.parseColor(t38 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t38= "+t38);
            }
        });
        button_t39.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t39=(t39+1)%2;      //点击然后改变flag的值
                button_t39.getBackground().setColorFilter(Color.parseColor(t39 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t39= "+t39);
            }
        });


        button_t40.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                t40=(t40+1)%2;      //点击然后改变flag的值
                button_t40.getBackground().setColorFilter(Color.parseColor(t40 >0?"RED":"#d6d7d7"),PorterDuff.Mode.DARKEN);
                Log.d(TAG,"——t40= "+t40);
            }
        });

    }
    @Override
    protected void onRestart() {
        super.onRestart();
//        mVideoView.start();
//        //监听视频播放完的代码
//        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mPlayer) {
//                // TODO Auto-generated method stub
//                mPlayer.start();
//                mPlayer.setLooping(true);           //循环播放
//            }
//        });
    }

//    public void onClick(View v) {
//        // TODO Auto-generated method stub
//        switch (v.getId()) {
//            case R.id.form_button_Connect:
//                Connect_WIFI_TCP("gzj1", "gzj123456", 2,"192.168.4.1", 8089);
//                Connect_WIFI_TCP("gzj2", "gzj123456", 2,"192.168.4.1", 8090);
//
//                break;
////            case R.id.form_button_t2:
////
////                break;
//            default:
//                break;
//        }
//    }

    @SuppressLint("PrivateResource")
    @Override
    public void onBackPressed() {       //重写back按键
        super.onBackPressed();

        overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
        wi=0;time=10;id=1;
        t1=0;t2=0;t3=0;t4=0;t5=0;t6=0;t7=0;t8=0;t9=0;t10=0;t11=0;t12=0;t13=0;t14=0;
        t15=0;t16=0;t17=0;t18=0;t19=0;t20=0;t21=0;t22=0;t23=0;t24=0;t25=0;t26=0;t27=0;t28=0;
//        mThreadPool.execute(new Runnable() {    // 利用线程池直接开启一个线程 & 执行该线程
//            @Override
//            public void run() {
//                try {
//
//                    outputStream = socket.getOutputStream();
//                    outputStream.write(("end"+"\n").getBytes("utf-8"));
//                    outputStream.flush();
//                    socket.close();             //断开TCP
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        });

        System.out.println("按下了back返回键，变量清零");
        return;
    }
}